<?php
namespace app\admin\controller;
use think\Db;
class User extends SessionCheck
{
    public function index()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $pages = pages('user',$where,input('get.page'));
        $data = Db::table('user')->where($where)->whereIn('aid',session('aid'))->limit($pages['limit'])->order('id desc')->select();
        
        return view('',['data' => $data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
    }

    public function edit()
    {
        $id = input('get.id')?input('get.id'):0;
        if(request()->isPost()) {
            $data = input('post.');
            if ($id) {
                if (empty($data['password'])){
                    unset($data['password']);
                } else {
                    $data['password'] = md5($data['password']);
                }
                Db::table('user')->where(['id' => $id])->update($data);
             }else{
               if(Db::table('user')->where(['phone' => $data['phone']])->where('id', '<>', $id)->find()) {
                    return error(lang('tip')['re_phone']);
                }
                $data['regtime'] = time();
                $data['password'] = md5($data['password']);
                $data['id'] = Db::table('user')->insertGetId($data);
                $id = $data['id'];
            }
        }
        $data = Db::table('user')->where(['id' => $id])->find();
        return view('',['data' => $data,'menu_list'=>$this->menu_list]);
    }

    public function del()
    {
        $id = input('get.id')?input('get.id'):0;
        $page = input('get.page');
        Db::table('user')->whereIn('aid',session('aid'))->delete($id);
        $this->redirect(url('/admin/user').'?page='.$page);
        exit;
    }

    public function opinion()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $pages = pages('opinion',$where,input('get.page'));
        $data = Db::table('opinion')->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('time desc')->select();
        foreach ($data as $k => $v){
            $data[$k]['username'] = Db::table('user')->where('id',$v['uid'])->value('name');
        }
        return view('',['data'=>$data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
    }
    //认证服务
    public function express_check()
    {
        $where = [];
        $aid = request()->request('aid');//区/县aid
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $where['express_type']=1;//司机认证
        if(request()->isPost()){
            $data = request()->post();
            $id = $data['id'];
            $uid = $data['uid'];
            unset($data['uid']);
            unset($data['id']);
            $info = Db::table('express_check')->where('id',$id)->whereIn('aid',session('aid'))->find();
            if($data['status'] == 1){
                Db::table('user')->where('id',$uid)->update(['isExpress'=>1,'idcard'=>$info['idcard'],'express_cut'=>'10','tip_cut'=>3,'true_name'=>$info['true_name'],'true_head'=>$info['true_head'],'ali_number'=>$info['ali_number'],'express_type'=>$info['express_type']]);
                $msg = '恭喜，您的实名认证已通过！';                                                                                                                                                                                       //express_type 司机、用户、酒店、
            }else{
                $msg = '抱歉，您的实名认证被拒绝！拒绝原因：'.$data['deny_cause'];
            }
            Db::table('express_check')->where('id',$id)->whereIn('aid',session('aid'))->update($data); //更新
            push_driver(md5($uid),json_encode(['type'=>2,'message'=>$msg,'parameters'=>'','status'=>15]));
            insert_message($uid,'实名认证',$msg,2);
        }else{
            $pages = pages('express_check',$where,input('get.page'));
            $data = Db::table('express_check')->where($where)->whereIn('aid',session('aid'))->limit($pages['limit'])->order('id desc')->select();
          
            foreach ($data as $k => $v){
                $data[$k]['true_name'] = $v['true_name'];//Db::table('user')->where('id',$v['uid'])->value('true_name');
                $data[$k]['express_type_name'] = Db::table('express_type')->where('id',$v['express_type'])->value('name');//
            }
            
            return view('',['data'=>$data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
        }
    }

    public function other()
    {
        $data = [];
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $tab = request()->get('tab');
        if(!empty($tab)){
            $post = request()->post();
            Db::table('express_config')->whereIn('aid',session('aid'))->where($where)->update($post);
        }
        if(!empty($aid)) {
            $data = Db::table('express_config')->whereIn('aid', session('aid'))->where($where)->find();
        }
        return view('',['data'=>$data,'menu_list'=>$this->menu_list]);
    }

}
